<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This page prints all participants or contacts who sents mail/s
 *
 * @package   block_email_list
 * @author    Toni Mas <toni.mas@uib.dot.es>
 * @copyright 2006 by <toni.mas at uib dot es>
 * @license   The source code packaged with this file is Free Software, Copyright (C) 2006 by
 *            <toni.mas at uib dot es>.
 *            It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
 *            You can get copies of the licenses here: http://www.affero.org/oagpl.html
 *            AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 * @uses $CFG
 * @uses $PAGE
 * @uses $OUTPUT
 */

global $CFG, $PAGE, $USER;

require_once('../../../config.php');
require_once($CFG->dirroot.'/blocks/email_list/email/lib.php');
require_once($CFG->libdir.'/ajax/ajaxlib.php');

$courseid = required_param('id', PARAM_INT);        // Course ID
$selgroup = optional_param('group', 0, PARAM_INT);  // Selected group
$roleid = optional_param('roleid', 0, PARAM_INT);   // Role ID
$page = optional_param('page', 0, PARAM_INT);       // Page
$perpage = optional_param('perpage', 7, PARAM_INT); // Max rows per page
$search = optional_param('search', '', PARAM_TEXT); // Searching users

$firstinitial = optional_param('fname', '', PARAM_ALPHA);  // Order by fistname
$lastinitial = optional_param('lname', '', PARAM_ALPHA);  // Order by lastname
$showlists = optional_param('showlists', 0, PARAM_INT);
if (empty($showlists)) {
    $showlists = 0;
}

// Get course, if exist
if (! $course = $DB->get_record('course', array('id' => $courseid))) {
    print_error('invalidcourseid', 'block_email_list');
}

require_login($course->id, false); // No autologin guest

if (! email_is_enabled_email_list($course->id)) {
    print_error('blocknotavailable', 'block_email_list');
}

if ($CFG->email_enable_ssl) {
    $wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
} else {
    $wwwroot = $CFG->wwwroot;
}

$PAGE->set_title(get_string('selectaction', 'block_email_list'));
$PAGE->set_url('/blocks/email_list/email/participants.php?id='. $courseid);
$PAGE->requires->css('/blocks/email_list/email/email.css');
$PAGE->requires->js('/blocks/email_list/email/manage.js');

echo $OUTPUT->header();

if ($CFG->email_old_select_participants) {
    //email_choose_course($courseid);

    email_choose_users_to_send($courseid, $roleid, $selgroup);
} else {

    //add the alphabetical search first
    echo get_string('alphabetical', 'block_email_list');

    echo '<div id="abcd">';
    echo '<ul>';
    //array of letters
    $alpha  = explode(',', get_string('alphabet', 'langconfig'));

    // First name
    echo '<li>'.get_string('firstname').': ';
    $nch = 0;

    foreach ($alpha as $ch) {

        if ($nch != 0) {
            echo ', ';
        }

        if ($ch == $firstinitial) {
            echo $ch;
        } else {
            echo "<a href=\"$wwwroot/blocks/email_list/email/participants.php?id=$courseid"
                ."&amp;group=$selgroup&amp;perpage=$perpage&amp;search=$search&amp;roleid=$roleid"
                ."&amp;lname=$lastinitial&amp;fname=$ch&amp;showlists=$showlists\">$ch</a>";
        }
        $nch++;
    }
    echo '</li>';

    // Lastname initial
    echo '<li>'.get_string('lastname').': ';
    $nch = 0;

    foreach ($alpha as $ch) {

        if ($nch!=0) {
            echo ', ';
        }

        if ($ch == $lastinitial) {
            echo $ch;
        } else {
            echo "<a href=\"$wwwroot/blocks/email_list/email/participants.php?id=$courseid"
                ."&amp;group=$selgroup&amp;perpage=$perpage&amp;search=$search&amp;roleid=$roleid"
                ."&amp;fname=$firstinitial&amp;lname=$ch&amp;showlists=$showlists\">$ch</a>";
        }
        $nch++;
    }

    echo '</li>';

    // Reset all param of the searching
    echo '<li>';
    echo '<a href="'.$wwwroot.'/blocks/email_list/email/participants.php?id='.$courseid.
         '&amp;group='.$selgroup.'&amp;showlists='.$showlists.'">'.get_string('allusersincourse', 'block_email_list').'</a>';
    echo '</li></ul></div>';

    // Search input
    $switch_plus = $OUTPUT->pix_url('/t/switch_plus');
    echo '<div onclick="switchMenu(\'srch\', \''. dirname($switch_plus) .'/\')">';
    echo '<img id="srch_icon" src="'. $switch_plus .'"/>';

    $spacer = array('height' => 1, 'width' => 4);
    echo $OUTPUT->spacer($spacer, false);

    echo get_string('searchparticipant', 'block_email_list').'</div>';

    echo '<div id="srch" style="display:none;">';
    echo '<form method="post" action="participants.php">';

    $attributes = array('name'      => 'search',
                        'value'     => $search,
                        'alt'       => get_string('searchparticipant', 'block_email_list'),
                        'size'      => 50,
                        'maxlength' => 150);
    echo html_writer::empty_tag('input', $attributes);

    echo '<input type="hidden" name="id" value="'.$courseid.'"/>';
    echo '<input type="hidden" name="group" value="'.$selgroup.'"/>';
    echo '<input type="hidden" name="page" value="'.$page.'"/>';
    echo '<input type="hidden" name="perpage" value="'.$perpage.'"/>';
    echo '<input type="hidden" name="roleid" value="'.$roleid.'"/>';
    echo '<input type="hidden" name="fname" value="'.$firstinitial.'"/>';
    echo '<input type="hidden" name="lname" value="'.$lastinitial.'"/>';
    echo '<input type="hidden" name="showlists" value="'.$showlists.'"/>';

    echo '<input type="submit" name="doit" value="'.get_string('search').'" />'
       . '</form>';
    echo '</div>';

    echo '<br />';

    $contextlevel = CONTEXT_COURSE;

    $sitecontext = context_system::instance();
    $context = context_course::instance($courseid);

    $rolenames = array();

    list($roles,) = get_mailable_roles($PAGE->context);

    foreach ($roles as $role) {
        $rolenames[$role->id] = strip_tags(role_get_name($role, $PAGE->context));   // Used in menus etc later on
    }

    /// If there are multiple Roles in the course, then show a drop down menu for switching
    if (count($rolenames) > 1) {
        echo '<div class="rolesform">';
        echo get_string('currentrole', 'role').': ';
        $rolenames = array(0 => get_string('all')) + $rolenames;
        echo $OUTPUT->single_select($PAGE->url, 'roleid', $rolenames, $roleid, '');
        echo '</div>';
    }

    // Prints group selector for users with a viewallgroups capability if course groupmode is separate
    echo '<br />';
    groups_print_course_menu($course, $wwwroot.'/blocks/email_list/email/participants.php?id='.$course->id);
    echo '<br /><br />';


    echo '<div id="participants"></div>'."\n".
         '<iframe id="idsearch" name="bssearch" src="get_users.php?id='.$courseid.'&amp;roleid='.$roleid.
         '&amp;group='.$selgroup.'&amp;page='.$page.'&amp;perpage='.$perpage.'&amp;search='.$search.
         '&amp;fname='.$firstinitial.'&amp;lname='.$lastinitial.'&amp;showlists='.$showlists.
         '" style="display:none;"></iframe>'."\n\n";

    if (has_capability('block/email_list:editsettings', $context) &&
        $DB->get_records('block_email_list_lists', array('owner' => $USER->id)) ) {
        // has_capability lists and has lists
        $OUTPUT->spacer($spacer, false);
        echo "<br /><img height=\"16\" width=\"16\" src=\"".($showlists ? "{$CFG->wwwroot}/blocks/email_list/email/images/delete.png" : "{$CFG->wwwroot}/blocks/email_list/email/images/add.png")."\"/>";
        echo '&nbsp;<a href="'.$wwwroot.'/blocks/email_list/email/participants.php?id='.$courseid.'&amp;group='.
             $selgroup.'&amp;page='.$page.'&amp;perpage='.$perpage.'&amp;search='.$search.'&amp;roleid='.$roleid.
             '&amp;fname='.$firstinitial.'&amp;lname='.$lastinitial.'&amp;showlists='.!$showlists.'">'.
             get_string($showlists ? 'hidelists' : 'showlists', 'block_email_list').'</a>'."\n";
    }

    echo $OUTPUT->spacer($spacer, false);

    if ($perpage == '7') {
        $linktext = get_string('showallusers');
        $linkpage = '99999';
        $linkimg  = 'add.png';

    } else {
        $linktext = get_string('showperpage', '', 7);
        $linkpage = 7;
        $linkimg  = 'delete.png';
    }
    echo '<div id="to_all_users" class="all_users"><img src="'.$CFG->wwwroot.
         '/blocks/email_list/email/images/'.$linkimg.'" height="16" width="16" alt="'.
         get_string('course').'" /> <a href="'.$wwwroot.'/blocks/email_list/email/participants.php?id='.
         $courseid.'&amp;group='.$selgroup.'&amp;perpage='.$linkpage.'&amp;search='.$search.
         '&amp;roleid='.$roleid.'&amp;fname='.$firstinitial.'&amp;lname='.$lastinitial.
         '&amp;showlists='.$showlists.'">'.$linktext.'</a></div>'."\n";
}
// Print close button
echo $OUTPUT->close_window_button();

echo $OUTPUT->footer();
